package com.yexin.mail.product.controller;

import com.yexin.common.utils.PageUtils;
import com.yexin.common.utils.R;
import com.yexin.common.vaild.AddGroup;
import com.yexin.common.vaild.UpdateGroup;
import com.yexin.common.vaild.UpdateStatusGroup;
import com.yexin.mail.product.entity.BrandEntity;
import com.yexin.mail.product.service.BrandService;
import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 品牌
 *
 * @author yexin
 * @email yexin@gmail.com
 * @date 2020-09-09 00:27:57
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand) {
//        if (result.hasErrors()) {
//            Map<String,String > map = new HashMap<>();
//            //1. 获取校验的错结果
//            result.getFieldErrors().forEach((item) -> {
//                // 获取错误的提示
//
//                item.getDefaultMessage();
//                //获取错误的名字
//            });
//            return R.error(400, "tijiao de shuju buhefa ");
//        } else {
        brandService.save(brand);
//        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
//        brandService.updateById(brand);

        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
